package com.ls.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 脱敏公用类
 *
 * @Author pengyu
 */
public class Desensitize {

    private static final Logger LOGGER = LoggerFactory.getLogger(Desensitize.class);
    public static final int NAME_MIN_LENGTH = 2;
    public static final int PHONE_MIN_LENGTH = 10;

    public static String getDesensitizeStr(String str, int type) {
        if (StringUtils.isBlank(str) || type < 0) {
            LOGGER.error("getDesensitizeStr param error :" + str + " " + type);
            return null;
        }
        //这么写可以少循环一次
        switch (type) {
            case 1:
                if (str.length() < NAME_MIN_LENGTH) {
                    return "*";
                }
                return getStarString(str, str.indexOf(1));
            case 2:
                if (str.length() < PHONE_MIN_LENGTH) {
                    LOGGER.error("getDesensitizeStr phone length < 10" + str);
                    return null;
                }
                return getStarString(str, 3, 7);
            default:
                break;
        }
        return null;
//        DesensitizeType desensitizeType = getDesensitizeType(type);
//        if (desensitizeType == null) {
//            LOGGER.error("getDesensitizeStr query result null :" + str + " " + type);
//            return null;
//        }
//        switch (desensitizeType) {
//            case NAME:
//
//                break;
//            case PHONE:
//
//                break;
//            default:
//                return null;
//        }
//        return null;
    }

    public enum DesensitizeType {
        //多个枚举值,注意名字并不是构造方法名
        NAME(1, "Name"), PHONE(2, "Phone");

        //枚举值所包含的属性
        private int type;
        private String name;

        //构造方法
        DesensitizeType(int key, String name) {
            this.setType(key);
            this.setName(name);
        }

        public static DesensitizeType getDesensitizeType(int type) {
            for (DesensitizeType enums : values()) {
                if (enums.type == type) {
                    return enums;
                }
            }
            return null;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 对字符串处理:将指定位置到指定位置的字符以星号代替
     *
     * @param content 传入的字符串
     * @param begin   开始位置
     * @param end     结束位置
     * @return
     */
    private static String getStarString(String content, int begin, int end) {

        if (begin >= content.length() || begin < 0) {
            return content;
        }
        if (end >= content.length() || end < 0) {
            return content;
        }
        if (begin >= end) {
            return content;
        }
        String starStr = "";
        for (int i = begin; i < end; i++) {
            starStr = starStr + "*";
        }
        return content.substring(0, begin) + starStr + content.substring(end, content.length());
    }
    /**
     * 对字符串处理:将指定位置到指定位置的字符以星号代替
     *
     * @param content 传入的字符串
     * @param begin   开始位置
     * @return
     */
    private static String getStarString(String content, int begin) {

        if (begin >= content.length() || begin < 0) {
            return content;
        }
        String starStr = "";
        for (int i = begin; i < content.length(); i++) {
            starStr = starStr + "*";
        }
        return content.substring(0, begin) + starStr ;
    }

    /**
     * 对字符加星号处理：除前面几位和后面几位外，其他的字符以星号代替
     *
     * @param content  传入的字符串
     * @param frontNum 保留前面字符的位数
     * @param endNum   保留后面字符的位数
     * @return 带星号的字符串
     */

    private static String getStarString2(String content, int frontNum, int endNum) {

        if (frontNum >= content.length() || frontNum < 0) {
            return content;
        }
        if (endNum >= content.length() || endNum < 0) {
            return content;
        }
        if (frontNum + endNum >= content.length()) {
            return content;
        }
        String starStr = "";
        for (int i = 0; i < (content.length() - frontNum - endNum); i++) {
            starStr = starStr + "*";
        }
        return content.substring(0, frontNum) + starStr
                + content.substring(content.length() - endNum, content.length());

    }

    public static void main(String[] args) {
        System.out.println(getStarString("13112345678", 3, 7));
        System.out.println(getStarString("0281234567", 3, 7));
        System.out.println(getStarString("刘一二", 1));

    }
}
